package org.eiahe.hr.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.vo.SysDeptVo;
import org.ehe.system.service.ISysDeptService;
import org.eiahe.hr.salary.domain.SocialInsurancePayment;
import org.eiahe.hr.salary.domain.bo.SocialInsurancePaymentBo;
import org.eiahe.hr.salary.domain.vo.SocialInsurancePaymentVo;
import org.eiahe.hr.salary.mapper.SocialInsurancePaymentMapper;
import org.eiahe.hr.salary.service.ISocialInsurancePaymentService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 社保缴纳Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class SocialInsurancePaymentServiceImpl implements ISocialInsurancePaymentService {

    private final SocialInsurancePaymentMapper baseMapper;
    private final ISysDeptService deptService;

    @Override
    public SocialInsurancePaymentVo queryById(Long paymentId) {
        SocialInsurancePaymentVo socialInsurancePaymentVo = baseMapper.selectVoById(paymentId);
        if(null != socialInsurancePaymentVo.getDepartment()){
            SysDeptVo sysDeptVo = deptService.selectDeptById(socialInsurancePaymentVo.getDepartment());
            if(Objects.nonNull(sysDeptVo)){
                socialInsurancePaymentVo.setDepartmentName(sysDeptVo.getDeptName());
            }
        }
        return socialInsurancePaymentVo;
    }

    @Override
    public List<SocialInsurancePaymentVo> queryList(SocialInsurancePaymentBo bo) {
        LambdaQueryWrapper<SocialInsurancePayment> lqw = buildQueryWrapper(bo);
        List<SocialInsurancePaymentVo> socialInsurancePaymentVos = baseMapper.selectVoList(lqw);
        socialInsurancePaymentVos.forEach(s->{
            if(null != s.getDepartment()){
                SysDeptVo sysDeptVo = deptService.selectDeptById(s.getDepartment());
                if(Objects.nonNull(sysDeptVo)){
                    s.setDepartmentName(sysDeptVo.getDeptName());
                }
            }
        });
        return socialInsurancePaymentVos;
    }

    @Override
    public TableDataInfo<SocialInsurancePaymentVo> queryPageList(SocialInsurancePaymentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SocialInsurancePayment> lqw = buildQueryWrapper(bo);
        Page<SocialInsurancePaymentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(s->{
            if(null != s.getDepartment()){
                SysDeptVo sysDeptVo = deptService.selectDeptById(s.getDepartment());
                if(Objects.nonNull(sysDeptVo)){
                    s.setDepartmentName(sysDeptVo.getDeptName());
                }
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 构建查询条件
     *
     * @param bo 社保缴纳业务对象
     * @return 查询条件
     */
    private LambdaQueryWrapper<SocialInsurancePayment> buildQueryWrapper(SocialInsurancePaymentBo bo) {
        LambdaQueryWrapper<SocialInsurancePayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()), SocialInsurancePayment::getEmployeeNo, bo.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeName()), SocialInsurancePayment::getEmployeeName,
                bo.getEmployeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getYearMonth()), SocialInsurancePayment::getYearMonth, bo.getYearMonth());
        // 注意：由于实体类字段结构已更改，BO中的insuranceType和paymentStatus字段不再直接映射
        // 如果需要根据保险类型或缴费状态查询，需要在BO中添加相应的字段或修改查询逻辑
        lqw.orderByDesc(SocialInsurancePayment::getCreateTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(SocialInsurancePaymentBo bo) {
        SocialInsurancePayment add = MapstructUtils.convert(bo, SocialInsurancePayment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPaymentId(add.getPaymentId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(SocialInsurancePaymentBo bo) {
        SocialInsurancePayment update = MapstructUtils.convert(bo, SocialInsurancePayment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SocialInsurancePayment entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
